const express = require('express'),
    router = express.Router(),
    Api = require('../models/api'),
    apix = new Api(),
    Tools = require('../models/Tools'),
    tool = new Tools();

let articleContent,
    article,
    articleComment,
    articleId,
    newarticle = {
        start:0,
        end:5
    };
router.get('/',(req, res, next) => {
    articleId = req.query.article;
    const array = [
        tool.promise(apix.getArticleById,articleId),
        tool.promise(apix.getNewArticle,newarticle),
        tool.promise(apix.getArticleCommentById,articleId)
    ];
    Promise.all(array).then(data => {
        articleContent = data[0][0];
        article = data[1];
        articleComment = data[2];
        res.render('article',{title:"文章详情 | 无虑Blog",articleContent:articleContent,article:article,articleComment:articleComment });
    });
});
router.post('/',(req,res) => {
    const commentid = req.body['id'],
        num =  articleContent.commentnum,
        number = parseInt(num)-1,
        id = articleContent['id'],
        array = [
            tool.promise(apix.deleteCommentById,commentid),
            tool.promise(apix.upDataArticleCommentNumber,{commentId:id,num:number})
        ];
    Promise.all(array).then(data => {
        articleComment = data[1];
        apix.getArticleCommentById(articleId,results => {
            articleComment = results;
            res.render('article',{title:"文章详情 | 无虑Blog",articleContent:articleContent,article:article,articleComment:articleComment });
        });
    });
});
module.exports = router;